package com.kevin.Code.String;

/**
 * @author Vinlee Xiao
 * @Classname ValidPalindrome
 * @Description Leetcode 125 验证回文串 简单 理清思路
 * @Date 2021/10/25 20:01
 * @Version 1.0
 */
public class ValidPalindrome {

    /**
     * @param s
     * @return
     */
    public boolean isPalindrome(String s) {

        String s1 = s.trim();

        int len = s1.length();
        if (len <= 1) {
            return true;
        }

        int left = 0;
        int right = len - 1;
        char[] ch = s1.toCharArray();

        while (left < right) {

            //排除其余符号的影响
            while (left < right && !Character.isDigit(ch[left]) && !Character.isLetter(ch[left])) {
                left++;
            }

            while (left < right && !Character.isDigit(ch[right]) && !Character.isLetter(ch[right])) {
                right--;
            }

            //判断两个字符是否相等
            if (left < right && Character.toUpperCase(ch[left]) != Character.toUpperCase(ch[right])) {

                return false;
            }
            left++;
            right--;
        }

        return true;
    }

    public static void main(String[] args) {
        ValidPalindrome validPalindrome = new ValidPalindrome();
        boolean b = validPalindrome.isPalindrome("A man, a plan, a canal: Panama");
        System.out.println(b);
    }
}
